package com.youxue.teachin.user.newuser.presenter;

import com.library.uiframework.mvp.BasePresenter;
import com.library.uiframework.utils.MToast;
import com.library.uiframework.utils.PatternUtils;
import com.library.uiframework.utils.StringUtils;
import com.youxue.teachin.common.vo.UserVO;
import com.youxue.teachin.user.newuser.INewUserView;
import com.youxue.teachin.user.newuser.usercase.NewUserUsercase;
import com.youxue.teachin.common.utils.DefaultSubscriber;
import com.youxue.teachin.user.repository.IUserRepository;
import rx.Observable;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-05 10:08
 * @version: 9.1.0
 */
public class NewUserPresenter extends BasePresenter<INewUserView> {
    NewUserUsercase mNewUserUsercase = new NewUserUsercase();
    /**
     * 构造方法
     *
     * @param iRegistUserView 绑定的视图对象
     */
    public NewUserPresenter(INewUserView iRegistUserView) {
        super(iRegistUserView);
    }

    public void getCaptcha() {
        String phone = mBindView.getPhone();
        if (!PatternUtils.isMobile(phone)) {
            MToast.getInstance().setText("手机号码输入不正确");
            return;
        }

       Observable<String> observable = mNewUserUsercase.getCaptcha(phone, IUserRepository.CAPTCHA_REQ);
        execute(observable, new DefaultSubscriber<String>() {
            @Override
            public void onNext(String s) {
                super.onNext(s);
                MToast.getInstance().setText("验证码发送成功");
                mBindView.setServiceCaptch(s);
            }
        });
    }

    public void createNewUser() {
        String phone = mBindView.getPhone();
        if (!PatternUtils.isMobile(phone)) {
            MToast.getInstance().setText("手机号码输入不正确");
            return;
        }
        String password = mBindView.getPassword();
        if (!PatternUtils.isPassword(password)) {
            MToast.getInstance().setText("密码支持大小写字母、数据");
            return;
        }
        String captcha = mBindView.getCaptach();
        if (StringUtils.isEmpty(captcha)) {
            MToast.getInstance().setText("请输入验证码");
            return;
        }

        execute(mNewUserUsercase.createNewUser(phone, password, captcha), new DefaultSubscriber<UserVO>(){
            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }

            @Override
            public void onNext(UserVO o) {
                super.onNext(o);
            }
        });
    }

}
